<?php

class UserController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction()
    {
		// Thay doi giao dien dang nhap
		$this->view->layout()->setLayout("login");
		$this->view->headTitle("Đăng nhập hệ thống");
		if($this->_request->isPost())
		{
			$username = $this->_request->getParam("TenTaiKhoan","");
			$password = $this->_request->getParam("MatKhau","");
            
			if($username != "")
			{
				$auth = Zend_Auth::getInstance();
				$authAdapter = new Zend_Auth_Adapter_DbTable(null,"TAIKHOAN","TenTaiKhoan","MatKhau");
				$authAdapter->setIdentity($username)
							->setCredential(md5($password));
				$result = $authAdapter->authenticate($authAdapter);
				if($result->isValid())
				{
					$store = $auth->getStorage();
					$user_info = $authAdapter->getResultRowObject(array(
						"MaTaiKhoan","TenTaiKhoan","MaNhom","HoTen"
					));
					$store->write($user_info);
					return $this->_redirect( $this->view->url(array('controller'=>'index','action'=>'index')) );
				}
				else
				{
					$this->view->alert = "Tài khoản hoặc mật khẩu không đúng.";
				}
			}
			else
			{
				$this->view->alert = "Tài khoản hoặc mật khẩu không đúng.";
			}
		}
    }

    public function logoutAction()
    {
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		return $this->_redirect( $this->view->url(array(
			'controller' => 'user', 
			'action' => 'login')
		));
    }

    public function noauthAction()
    {
        // action body
        $this->_helper->layout()->setLayout("noauth");
        $this->view->headTitle("Cấm quyền truy cập");
    }


}







